<template>
  <view class="main">
    <view :style="{'height':topheight+'px'}"
          class="topbar">
      <view class="topTitle"
            style="font-weight: 500;
font-size: 36rpx;
color: #FFFFFF;"
            :style="{'top':(topheight - 26)+'px'}"> 案例 </view>
    </view>
    <view class="toptab ">
      <view :class="item.id==current?'act':''"
            class="tab_item"
            v-for="(item,index) in tabList"
            :key="item.id"
            @click="changetab(item.id)">
        <view class="">
          {{item.name}}
        </view>
      </view>
    </view>
    <view class="list"
          :style="{'height':`calc(100vh - 90rpx - ${topheight*2}rpx - env(safe-area-inset-bottom))`}">
      <z-paging ref="paging"
                v-model="list"
                @query="queryList"
                :fixed="false"
                :auto-clean-list-when-reload="false"
                :auto="true"
                :paging-style="{ height:'100%'}"
                :show-refresher-when-reload="true"
                :default-page-size="10"
                :hide-empty-view="true">
        <!-- v-if="current===0" -->
        <view class="box">
          <view class="box_item"
                v-for="(item,index) in list"
                :key="index">
            <view class="video">
              <!-- 生成中 -->
              <image src="../../static/work/clone.png"
                     v-if="item.status===1 || item.status===2"
                     mode=""
                     style="width: 100%;height: 486rpx;"
                     class="video">
              </image>
              <!-- 成功 -->
              <image :src="item.coverUrl+OssVideoShrink"
                     v-if="item.status===3"
                     mode="aspectFill"
                     @click="previewVideo(item)"
                     style="width: 330rpx;border-top-right-radius: 10rpx;border-top-left-radius: 10rpx;"
                     class="video">
              </image>
              <!-- 失败 -->
              <view class=""
                    v-if="item.status===4"
                    style="width: 100%;height: 486rpx;background-color: #202020;padding: 140rpx 40rpx 0;border-top-right-radius: 10rpx;border-top-left-radius: 10rpx;"
                    class="zl-flex-col zl-col-center">
                <image src="../../static/clone/error.png"
                       mode=""
                       style="width: 62rpx;height: 62rpx;"
                       class="video">
                </image>
                <view class="zl-color-white zl-font-24 zl-m-t-20"
                      style="opacity:0.8 ;"> 克隆失败 </view>
                <view class="zl-color-999 zl-font-22 zl-m-t-50"
                      style="opacity:0.6 ;">
                  原因：{{item.reason}}
                </view>
              </view>
              <image src="../../static/work/play.png"
                     v-if="item.status===3"
                     mode=""
                     class="play"
                     @click="previewVideo(item)"
                     style="width: 70rpx;height: 70rpx;">
              </image>
              <!-- <view class="time">
							时长：{{item.step}}{{'/'+item.maxStep}}
						</view> -->
            </view>
            <view class="content">
              <view class="zl-color-000 zl-font-26">
                {{item.name}}
              </view>
              <view class="zl-color-999 zl-font-20 zl-m-t-4">
                {{$util.fomateDate(item.createTime/1000)}}
              </view>
            </view>
          </view>
          <view v-if="list.length<=0"
                class=" zl-flex-col zl-col-center"
                style="width: 690rpx;margin-top: 50%;">
            <image src="../../static/work/empty.png"
                   mode=""
                   style="width: 230rpx;height: 250rpx;margin-bottom: 48rpx;"></image>
            <text class="zl-font-30 zl-color-666">空空如也，您还没有创建哦~</text>
          </view>
        </view>
      </z-paging>
      <uni-popup ref="alertDialog"
                 type="dialog">
        <uni-popup-dialog type="error"
                          cancelText="关闭"
                          confirmText="同意"
                          title="温馨提示"
                          :content="content"
                          @confirm="submitConfirm"
                          @close="reject"></uni-popup-dialog>
      </uni-popup>
      <uni-popup background-color="#fff"
                 ref="pop2"
                 type="bottom"
                 border-radius="20rpx 20rpx 0 0">
        <view class="pop-box">
          <view class="pop-top zl-flex zl-row-between">
            <view class="video_poo_title">预览素材</view>
            <image @click="closeVideo()"
                   style="width: 30rpx;"
                   src="/static/icon/pop_close.png"
                   mode="widthFix">
            </image>
          </view>
          <view class="popup-video">
            <video v-if="isVideo"
                   :src="activeVideo"
                   :controls="true"></video>
          </view>
          <view class="btn_box zl-flex zl-row-between">
            <!-- <view @click="del_video(activeIndex,activeVideo)"
						class="save_btn zl-theme-btn-out zl-flex zl-row-center zl-font-32 zl-font-bold">
						删除
					</view> -->
            <!-- <view @click="downloadfile(activeVideo)"
							class="save_btn zl-theme-btn-1 zl-flex zl-row-center zl-font-32 zl-font-bold"> 下载 </view> -->
          </view>
        </view>
      </uni-popup>
      <PrivacyPoup></PrivacyPoup>
    </view>
  </view>
</template>
<script>
import PrivacyPoup from '@/components/PrivacyPoup.vue';
export default {
  components: {
    PrivacyPoup,
  },
  data () {
    return {
      tabList: [{
        id: 1,
        name: '新闻大幅度发放'
      }, {
        id: 2,
        name: '推荐 官方'
      }, {
        id: 3,
        name: '带货反反复复凤飞飞'
      }, {
        id: 4,
        name: '测试'
      }],
      current: 1,
      list: [],
      content: '',
      clipTaskId: 0, // 删除id
      content: '',
      OssVideoShrink: getApp().globalData.OssVideoShrink,
      ossVideoCover: getApp().globalData.ossVideoCover,
      isVideo: false,
      activeVideo: '',
      topheight: '',
    };
  },
  async onShow () {
    await this.$onLaunched;
    const isReload = getApp().globalData.isReload;
    if (isReload) {
      this.$refs.paging.reload();
      getApp().globalData.isReload = false
    }
  },
  onLoad () {
    this.topheight = getApp().globalData.topBarHeight;
  },
  onShareAppMessage (res) {
    return {
      title: getApp().globalData.shareTitle,
      imageUrl: getApp().globalData.shareImg,
      path: '/pages/index/index',
    };
  },
  methods: {
    changetab (tab) {
      this.current = tab;
      console.log(this.current);
      this.$refs.paging.clear()
      this.$refs.paging.reload();
    },
    // 取消删除	
    previewVideo (item) {
      this.isVideo = true;
      this.api.userfile.geturl({
        userFileId: item.userFileId
      }).then(res => {
        if (res.code === 0) {
          this.activeVideo = res.data.url;
          this.$refs.pop2.open()
        } else {
          this.$util.msg(res.msg);
        }
      }).catch(err => {
        this.$util.msg('请检查网络连接！');
      })
    },
    closeVideo () {
      setTimeout(() => {
        this.activeVideo = ''
        this.isVideo = false;
      }, 250);
      this.$refs.pop2.close()
    },
    reject () {
      this.$refs.alertDialog.close()
    },
    async downloadfile () {
      console.log(this.activeVideo);
      if (!this.activeVideo) {
        this.$util.msg('视频还未加载完哦！');
        return;
      }
      try {
        var res = await this.$mp_mini.authorize('scope.writePhotosAlbum', '保存到相册');
        // console.log(res);
      } catch (e) {
        this.$util.msg(e.message);
        return;
      }
      uni.showLoading({
        title: '正在下载...'
      });
      uni.downloadFile({
        url: this.activeVideo,
        success: data => {
          if (data.statusCode === 200) {
            uni.saveVideoToPhotosAlbum({
              filePath: data.tempFilePath,
              success: () => {
                uni.hideLoading();
                this.$util.msg('保存成功~');
              },
              fail: er => {
                uni.hideLoading();
                this.$util.msg('视频保存失败！');
              }
            });
          } else {
            uni.hideLoading();
            this.$util.msg('视频下载失败！');
          }
        },
        fail: err => {
          uni.hideLoading();
          this.$util.msg('视频下载失败！');
        }
      });
    },
    // 打开删除弹窗
    del (item) {
      this.clipTaskId = item.iclipTaskId
      this.content = '确认删除' + item.name + '吗？'
      this.$refs.alertDialog.open()
    },
    // 确认删除
    submitConfirm () { },
    queryList (page, limit) {
      let par = {
        page: page,
        limit: limit,
        status: 0,
        // target: this.current === 0 ? 2 : 1
        target: 2
      }
      this.api.clip.getcliptasklist(par).then(res => {
        if (res.code === 0) {
          this.$refs.paging.complete(res.data.list);
        } else {
          // let arr = [{
          // 		createTime: '2023年5月10日16:09:33',
          // 		name: '555',
          // 		status: 2
          // 	},
          // 	{
          // 		createTime: '2023年5月10日16:09:33',
          // 		name: '555',
          // 		status: 3
          // 	},
          // 	{
          // 		createTimetime: '2023年5月10日16:09:33',
          // 		name: '555',
          // 		status: 1
          // 	},
          // 	{
          // 		createTimetime: '2023年5月10日16:09:33',
          // 		name: '555',
          // 		status: 4
          // 	}
          // ]
          // this.$refs.paging.complete(arr);
          this.$util.msg(res.msg);
          this.$refs.paging.complete(false);
        }
      }).catch(err => {
        this.$util.msg('请检查网络连接！');
        this.$refs.paging.complete(false);
      })
    },
  }
}
</script>
<style lang="scss" scoped>
@import "css/my_bg.scss";

::-webkit-scrollbar {
  height: 0;
}

.main {
  width: 100%;
  height: 100vh;
  background: url($bg) no-repeat;
  background-size: 100% 101%;
  // padding: 30rpx;
}

.toptab {
  width: 100%;
  height: 90rpx;
  padding: 20rpx 30rpx;
  overflow-x: scroll;
  overflow-y: hidden;
  white-space: nowrap;

  .tab_item {
    display: inline-block;
    padding: 0 30rpx;
    height: 50rpx;
    line-height: 46rpx;
    border-radius: 25rpx;
    border: 2rpx solid #ffffff;
    opacity: 0.2;
    font-size: 26rpx;
    color: #ffffff;
    margin-right: 20rpx;
  }

  .act {
    border: 2rpx solid #ffffff !important;
    color: #ffffff !important;
    opacity: 1 !important;
  }
}

.list {
  width: 750rpx;
  // height: calc(100vh - 90rpx);
  height: 100vh;

  .box {
    width: 750rpx;
    padding: 30rpx;
    display: flex;
    flex-direction: row;
    gap: 30rpx;
    flex-wrap: wrap;

    .box_item {
      width: 330rpx;

      .video {
        width: 100%;
        height: 486rpx;
        position: relative;

        .play {
          position: absolute;
          left: 50%;
          top: 50%;
          transform: translate(-50%, -50%);
        }

        .time {
          position: absolute;
          bottom: 0;
          left: 0;
          width: 100%;
          height: 40rpx;
          background: #000000;
          opacity: 0.5;
          padding-left: 28rpx;
          font-size: 20rpx;
          color: #ffffff;
          line-height: 40rpx;
        }
      }

      .content {
        background-color: #fff;
        padding: 24rpx 30rpx;
        border-radius: 0rpx 0rpx 10rpx 10rpx;
      }
    }
  }

  .box1 {
    width: 690rpx;
    border-radius: 20rpx;

    .box_item1 {
      padding: 40rpx 30rpx;
      margin-top: 30rpx;
      display: flex;
      flex-direction: column;
      background: #ffffff;
      border-radius: 20rpx;

      .line {
        margin-top: 40rpx;
        width: 633rpx;
        height: 1rpx;
        border: 1rpx solid #979797;
        opacity: 0.2;
      }

      .content {
        margin-top: 30rpx;
        font-size: 24rpx;
        color: #999999;
        overflow: hidden;
        text-overflow: ellipsis;
        display: -webkit-box;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
      }
    }
  }

  .download {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 121rpx;
    height: 56rpx;
    background: #ffffff;
    border-radius: 10rpx;
    border: 1rpx solid #000000;
    font-weight: 400;
    font-size: 24rpx;
    color: #000000;
  }

  .edit {
    margin-left: 24rpx;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 121rpx;
    height: 56rpx;
    background: #ffffff;
    border-radius: 10rpx;
    border: 1rpx solid red;
    font-weight: 400;
    font-size: 24rpx;
    color: red;
  }
}

.pop-box {
  background-color: #fff;
  border-radius: 20rpx 20rpx 0 0;

  .pop-top {
    height: 120rpx;
    padding: 0 30rpx;
    border-bottom: 1px solid #ddd;
    font-family: PingFangSC, PingFang SC;
    font-weight: 500;
    font-size: 36rpx;
    color: #131415;
  }
}

.popup-video {
}

.popup-video video {
  width: 100%;
  height: 60vh;
}

.btn_box {
  width: 750rpx;
  height: 128rpx;
  background: #ffffff;
  box-shadow: 0 -3rpx 9rpx 0 rgba(0, 0, 0, 0.1);
  padding: 0 30rpx;
  position: relative;
  z-index: 9;

  .save_btn {
    width: 690rpx;
    height: 88rpx;
    border-radius: 44rpx;
  }
}

.topTitle {
  position: absolute;
  top: 0;
  left: 42rpx;
  width: 223rpx;
}
</style>